<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
    <title>发送通知</title>
    <!-- Bootstrap -->
    <link href="/SmartCommunity/css/pc/fileinput.css" media="all" rel="stylesheet" type="text/css" />	
	<link href="/SmartCommunity/css/pc/bootstrap.css" rel="stylesheet">
	<link href="/SmartCommunity/css/pc/main.css" rel="stylesheet">
	
  </head>
  <body>
<nav class="navbar navbar-inverse navbar-fixed-top">
	<div class="container">
		<div class="navbar-header">
			<button type="button" class="navbar-toggle collapsed"
				data-toggle="collapse" data-target="#bs-example-navbar-collapse-1"
				aria-expanded="false">
				<span class="sr-only">Toggle navigation</span> <span
					class="icon-bar"></span> <span class="icon-bar"></span> <span
					class="icon-bar"></span>
			</button>
			<a class="navbar-brand  smart" href="/SmartCommunity/views/pc/smart.html">SMART</a>
		</div>
		<!-- Collect the nav links, forms, and other content for toggling -->
		<div class="collapse navbar-collapse"
			id="bs-example-navbar-collapse-1">
			<ul class="nav navbar-nav">
			</ul>
			<form class="navbar-form navbar-right">
				<a class="btn btn-primary" id="quit">退出</a>
			</form>
		</div>
	</div>
	</nav>
    
	<div class="greywrap">
      <div class="callout">   
		<h3>发送小区通知<a class="send_title_a">选择模板</a></h3>
     <form role="form" class="form-horizontal">
     			<div class="form-group" id="tem_name">
                    <label class="col-md-3 control-label" for="name">模板名称</label>
                    <div class="col-md-6">
                        <input class="form-control" name="name" type="text" id="name" placeholder="请输入模板名称..." value="" />
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-md-3 control-label" for="title">标题</label>
                    <div class="col-md-6">
                        <input class="form-control" name="title" type="text" id="title" placeholder="请输入标题..." value="" />
                    </div>
                </div>
                <div class="form-group">
                    <label for="intro" class="control-label col-md-3">内容</label>
                    <div class="col-md-6">
                       <div id="editor" type="text/plain" style="width:100%;height:300px;" ></div>
                    </div>
                </div> 
                <div class="form-group">
                    <div class="col-md-offset-3 col-md-6">
                    <button id="add_template" type="button" class="btn btn-primary btn-sm">
                            添加模板
                        </button>
                        <button id="send_notice" type="button" class="btn btn-primary btn-sm">
                            发 送
                        </button>

                    </div>
                </div>
            </form>
  </div>
</div> 

<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog"
		aria-labelledby="exampleModalLabel">
		<div class="modal-dialog" role="document">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal"
						aria-label="Close">
						<span aria-hidden="true">&times;</span>
					</button>
					<h4 class="modal-title" id="exampleModalLabel">选择模板消息</h4>
				</div>
				<div class="modal-body">
					<form>
						<div class="form-group">
							<label for="modify_type" class="control-label">消息类型</label>
							<div>
   <label class="checkbox-inline">
      <input type="radio" name="optionsRadiosinline" id="optionsRadios3" 
         value="0" checked> 自定义模板
   </label>
   <label class="checkbox-inline">
      <input type="radio" name="optionsRadiosinline" id="optionsRadios4" 
         value="1"> 添加模板
   </label>
      <label class="checkbox-inline">
      <input type="radio" name="optionsRadiosinline" id="optionsRadios5" 
         value="2"> 选择模板
   </label>
</div>
						</div>
						<div class="form-group" >
							<label for="modify_type" class="control-label">类型</label> <span class="imp error_type" style="display:none;">请选择模板类型</span><select
								class="form-control" id="modify_type" disabled="disabled">
								<option value="">请选择</option>
							</select>
						</div>
					</form>
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
					<button type="button" id="btn_sure" class="btn btn-primary"
						onclick="submitOK(this)">确定</button>
				</div>
			</div>
		</div>
	</div>
	
 
	<script src="/SmartCommunity/js/pc/jquery.js"></script>
	<script src="/SmartCommunity/js/pc/init.js"></script>
    <script src="/SmartCommunity/js/pc/bootstrap.js"></script>
	 <script src="/SmartCommunity/js/pc/jquery.atmosphere.js"></script>
    <script src="/SmartCommunity/js/pc/form.js" type="text/javascript" charset="utf-8"></script>
	 <script type="text/javascript" charset="utf-8" src="/SmartCommunity/ueditor/ueditor.config.js"></script>
    <script type="text/javascript" charset="utf-8" src="/SmartCommunity/ueditor/ueditor.all.js"> </script>
     <script type="text/javascript" charset="utf-8" src="/SmartCommunity/ueditor/lang/zh-cn/zh-cn.js"></script>
	 <script type="text/javascript" charset="utf-8">
		init();
		$.ajax({
			type : "POST",
			async : false,
			url : "/SmartCommunity/pc/get_template_list",
			contentType : "application/json;charset=utf-8",
			success : function(data) {
				var arr= eval(data);
				var type=$("#modify_type");
				for (var i = 0; i < arr.length; i++) {
					type.append("<option <option class=\'option_tem\' value=\'"+arr[i].id+"\'>"
									+ arr[i].name+ "</option>");
				}
			},
			error : function() {
				alert("error");
				var type=$("#modify_type");
				arr=[{"value":0,"type":"a"},{"value":1,"type":"b"}];
				for (var i = 0; i < arr.length; i++) {
					type.append("<option <option class=\'option_tem\' value=\'"+arr[i].id+"\'>"
									+ arr[i].name
									+ "</option>");
				}
			}
		});
		var modify_type=$("#modify_type").val("");
		$('#exampleModal').modal('show');
		var ue = UE.getEditor('editor');
		
		$("#add_template").click(function(){
			var arr = [];
	        arr.push(ue.getContent());
	        var name=$("#name").val();
	        var title=$("#title").val();
	        var content=$("#content").val();
	        if(name==""){
	        	$("#title").focus();
	        	alert("模板名称不能为空");
	        	return false;	        	
	        }
	        if(title==""){
	        	$("#name").focus();
	        	alert("标题不能为空");
	        	return false;	        	
	        }
	        var temp=ue.hasContents();
	        if(!temp){
	        	alert("内容不能为空");
	        	ue.focus();
	        	return false;	        	
	        }
	        $.ajax({
				type:"POST",
				url:"/SmartCommunity/pc/add_template",
				//contentType:"application/json;charset=utf-8",
				data:{name:name, title:title, content:arr},
				success:function(data){
					if(data=="true"){
						alert("添加模板成功！");
						$("#name").val("");
						ue.setContent("");
						$("#title").val("");
					}
					else{
						alert("添加模板失败！");
					}
				},
				error:function(){
					alert("error");
				}
			});
		});
		
		$("#send_notice").click(function(){
			var arr = [];
	        arr.push(ue.getContent());
	        var title=$("#title").val();
	        var only_text=ue.getContentTxt();
	        alert(only_text);
	        //var format_text_html=ue.getContent();
	       // alert(format_text_html);
	        //var format_text=ue.getPlainTxt();
	        //alert(format_text);
	        var text=only_text.substring(0, 100);
	        alert(text);
	        if(title==""){
	        	$("#title").focus();
	        	alert("标题不能为空");
	        	return false;	        	
	        }
	        var temp=ue.hasContents();
	        if(!temp){
	        	alert("内容不能为空");
	        	ue.focus();
	        	return false;	        	
	        }
	        $.ajax({
				type:"POST",
				url:"/SmartCommunity/pc/add_notice",
				data:{title:title,content:arr,summary:only_text},
				//contentType:"application/json;charset=utf-8",
				success:function(data){
					alert("data:"+data);
					if(data=="true"){
						alert("发送通知成功！");
						$("#title").val("");
						ue.setContent("");
					}
					else{
						alert("发送通知失败！");
					}
				},
				error:function(){
					alert("error");
				}
			});
		});
		
		$(".send_title_a").click(function(){
			$("#name").val("");
			ue.setContent("");
			$("#title").val("");
			$(".option_tem").remove();
			$('#exampleModal').modal('show');
			$.ajax({
				type : "POST",
				async : false,
				url : "/SmartCommunity/pc/get_template_list",
				contentType : "application/json;charset=utf-8",
				success : function(data) {
					var arr= eval(data);
					var type=$("#modify_type");
					for (var i = 0; i < arr.length; i++) {
						type.append("<option class=\'option_tem\' value=\'"+arr[i].id+"\'>"
										+ arr[i].name
										+ "</option>");
					}
				},
				error : function() {
					alert("error");
					var type=$("#modify_type");
					arr=[{"value":0,"type":"a"},{"value":1,"type":"b"}];
					for (var i = 0; i < arr.length; i++) {
						type.append("<option <option class=\'option_tem\' value=\'"+arr[i].id+"\'>"
										+ arr[i].name
										+ "</option>");
					}
				}
			});
		});
		
		function submitOK(e){
			var type_radio=$("input[type='radio']:checked").val();
			var modify_type=$("#modify_type").val();
			if(type_radio==0){
				$("#tem_name").css("display","none");
				$("#send_notice").css("display","inline");
				$("#add_template").css("display","none");
				$('#exampleModal').modal('hide');
				
			}
			else if(type_radio==1){
				$("#tem_name").css("display","block");
				$("#send_notice").css("display","none");
				$("#add_template").css("display","inline");
				$('#exampleModal').modal('hide');
			}
			else if(type_radio==2&&modify_type==''){
				$(".error_type").css("display","inline");
				return false;
			}
			else{
				$(".error_type").css("display","none");
				$.ajax({
					type:"POST",
					url:"/SmartCommunity/pc/get_template",
					contentTpye:"application/json;charset=utf-8",
					data:{id:modify_type},
					success:function(data)
					{
						$("#tem_name").css("display","none");
						$("#send_notice").css("display","inline");
						$("#add_template").css("display","none");
						var arr=eval("("+data+")");
						$("#title").val(arr.title);
						ue.setContent(arr.content);
						$('#exampleModal').modal('hide');
						
					},
					error:function(data)
					{
						alert("error");
						$("#tem_name").css("display","none");
						$("#send_notice").css("display","inline");
						$("#add_template").css("display","none");
						$("#title").val("arr.title");
						ue.setContent('arr.content');
						$('#exampleModal').modal('hide');
					}
				});
			}
			
		}
		
		$("#optionsRadios5").click(function(){
			$("#modify_type").attr("disabled",false);
		});
		$("#optionsRadios3").click(function(){
			$("#modify_type").attr("disabled",true);
		});
		$("#optionsRadios4").click(function(){
			$("#modify_type").attr("disabled",true);
		});
    </script>
  </body> 
</html>